<?php
/**
 * Created by PhpStorm.
 * User: HS032
 * Date: 2020/8/24
 * Time: 18:12
 */

namespace ticket\modules\frontend\controllers;


use ticket\libs\CheckSign;
use ticket\libs\Tools;
use ticket\modules\frontend\models\TicketCategory;
use ticket\modules\frontend\models\TicketInfo;
use ticket\modules\frontend\models\TicketType;
use Yii;
use yii\base\Exception;
use yii\web\Controller;

class TicketInfoController extends Controller
{

    /**** 门票类别
     * @return false|string
     */
    public function actionTicketTypeInfo()
    {
        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            $app_id = isset($post['appId'])?$post['appId']:'';
            if  (empty($app_id)){
                return Tools::showRes(10404,'参数有误');
            }

            if (isset($post['name']) and !empty($post['name'])){
                $where = ['like','type_name',$post['name']];
            }else{
                $where = [];
            }

            $ticketTypeInfo = TicketType::find()->where('app_id = :id',[':id' => $app_id])->andWhere($where)->asArray()->all();

            if (empty($ticketTypeInfo)){
                $ticketTypeInfo = [];
            }else{
                foreach ($ticketTypeInfo as $key => $val){
                    $ticketTypeInfo[$key]['image_url'] = json_decode($val['image_url'],true)[0]['url'];
                }
            }

            return Tools::showRes(0,'success',[
                'ticketType' => $ticketTypeInfo,
            ]);

        }
    }


    /*** 门票列表
     * @return false|string
     */
    public function actionTicketList()
    {
        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            try{
                if (!isset($post['appId']) or empty($post['appId'])){
                    throw new Exception('appId不能为空','10404');
                }

                if (isset($post['name']) and !empty($post['name'])){
                    $name = $post['name'];
                    $whereName = ['like','tk_ticket_info.name',$name];
                }else{
                    $whereName = [];
                }
                if (isset($post['type']) and !empty($post['type'])){
                    $whereType= ['tk_ticket_info.type_id' => $post['type']];
                }else{
                    $whereType= [];
                }

                $ticketInfo = TicketCategory::find()->where(['tk_ticket_info.state' => 1])->andWhere($whereType)->andFilterWhere($whereName)->andFilterWhere(['tk_ticket_info.app_id' => $post['appId']])->joinWith(['ticket'])->asArray()->all();
                return Tools::showRes(0,'success',[
                    'ticketList' => $ticketInfo,
                ]);

            }catch (Exception $e){
                return Tools::showRes($e->getCode(),$e->getMessage());
            }
        }
    }


    public function actionTicketCategoryList()
    {
        $ticketInfo = TicketCategory::find()->where(['tk_ticket_info.state' => 1])->joinWith(['ticket'])->asArray()->all();
        var_dump($ticketInfo);die;
    }
    
    

    public function actionTicketDetails()
    {
        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            $tid = isset($post['ticket_id'])?$post['ticket_id']:'';
            if (empty($tid)){
                return Tools::showRes(10404,'参数有误');
            }
            $serarchModel = TicketInfo::find()->where('tk_ticket_info.tid = :id',[':id' => $tid]);
            $ticketInfo = $serarchModel->joinWith(['additional'])->orderBy(['tid' => SORT_DESC])->asArray()->one();

            if (empty($ticketInfo)){
                return Tools::showRes(10405,'门票信息不存在');
            }

            return Tools::showRes(0,'success',[
                'ticketInfo' => $ticketInfo,
            ]);
        }
    }

    public function actionGetMemeberId()
    {
        if(isset(Yii::$app->session->get('memberInfo')->member_id)){
            $member_id = Yii::$app->session->get('memberInfo')->member_id;
            $integral = Yii::$app->session->get('memberInfo')->integral;
            $integral = 5000;
            $wechat_data = Yii::$app->session->get('memberInfo')->wechat;
            $openid = $wechat_data->wechat_openid;


            var_dump($member_id,$integral,$openid);die;
        };
        return 000;
    }

}